﻿using System.Linq;

namespace EFS.Common.UI
{
	public static class Misc
	{
		/// <summary>
		/// Check
		/// </summary>
		/// <param name="checkBox"></param>
		/// <returns></returns>
		public static System.Boolean Ischecked(System.Windows.Controls.CheckBox checkBox)
		{
			return checkBox.IsChecked.HasValue && checkBox.IsChecked.Value;
		}

		/// <summary>
		/// Check
		/// </summary>
		/// <param name="checkBox"></param>
		/// <returns></returns>
		public static System.Boolean Ischecked(System.Windows.Controls.RadioButton radioButton)
		{
			return radioButton.IsChecked.HasValue && radioButton.IsChecked.Value;
		}

		/// ClearAllChildren
		public static void ClearAllChildren(
			System.Windows.Controls.Grid grid,
			System.Int32 columnIndex,
			System.Int32 rowIndex)
		{
			// iterate
			while (true)
			{
				var control =
					(from d in grid.Children
					 where System.Windows.Controls.Grid.GetColumn(d as System.Windows.FrameworkElement) == columnIndex
						 && System.Windows.Controls.Grid.GetRow(d as System.Windows.FrameworkElement) == rowIndex
					 select d).FirstOrDefault();

				// if no child left, break operation
				if (null == control)
				{
					break;
				}

				// remove
				grid.Children.Remove(control);
			}
		}

		/// SetTableHeading
		public static void SetTableRowAlternateBackground1(
			System.Windows.Controls.Grid grid,
			System.Boolean firstLevel,
			System.Boolean alternate,
			System.Int32 rowIndex,
			System.Int32 columnIndex,
			System.Int32 columnSpan)
		{
			System.Windows.Shapes.Rectangle rectangle = new System.Windows.Shapes.Rectangle();
			rectangle.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
			rectangle.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
			rectangle.Margin = new System.Windows.Thickness(5, 0, 5, 0);
			rectangle.Fill = firstLevel ?
				new System.Windows.Media.SolidColorBrush(alternate ? System.Windows.Media.Colors.Purple: System.Windows.Media.Colors.Green):
				new System.Windows.Media.SolidColorBrush(alternate ? System.Windows.Media.Colors.Yellow: System.Windows.Media.Colors.Blue);
			rectangle.Fill.Opacity = 0.1;

			// add to grid
			grid.Children.Add(rectangle);
			System.Windows.Controls.Grid.SetRow(rectangle, rowIndex);
			System.Windows.Controls.Grid.SetColumn(rectangle, 0);
			System.Windows.Controls.Grid.SetColumnSpan(rectangle, columnSpan);
		}

		/// SetTableHeading
		public static void SetTableHeading(
			System.Windows.Controls.Grid grid,
			System.Int32 columnIndex,
			System.Int32 rowIndex = 0)
		{
			// create border
			System.Windows.Controls.Border border = new System.Windows.Controls.Border();
			border.BorderThickness = new System.Windows.Thickness(1);
			border.BorderBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Gray);
			border.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
			border.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
			border.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.LightGray);
			border.Background.Opacity = 0.5;

			// add to grig
			grid.Children.Add(border);
			System.Windows.Controls.Grid.SetRow(border, rowIndex);
			System.Windows.Controls.Grid.SetColumn(border, columnIndex);
		}

		/// SetTableHeading
		public static void SetTableSectionSplitter(
			System.Windows.Controls.Grid grid,
			System.Int32 columnIndex,
			System.Int32 rowIndex)
		{
			// create border
			System.Windows.Controls.Border border = new System.Windows.Controls.Border();
			border.BorderThickness = new System.Windows.Thickness(1);
			border.BorderBrush = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.White);
			border.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
			border.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
			border.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.LightGray);

			// add to grig
			grid.Children.Add(border);
			System.Windows.Controls.Grid.SetRow(border, rowIndex);
			System.Windows.Controls.Grid.SetColumn(border, columnIndex);
		}

		/// SetTableHeading
		public static void SetTableSectionSplitter(
			System.Windows.Controls.Grid grid,
			System.String text,
			System.Int32 columnIndex,
			System.Int32 rowIndex)
		{
			SetTableSectionSplitter(grid, columnIndex, rowIndex);
			
			// test text
			if (EFS.Common.Verify.String(text))
			{
				// create text block
				System.Windows.Controls.TextBlock textBlockText = new System.Windows.Controls.TextBlock();
				textBlockText.Foreground = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Blue);
				textBlockText.Text = text;
				textBlockText.VerticalAlignment = System.Windows.VerticalAlignment.Center;
				textBlockText.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

				// add to grig
				grid.Children.Add(textBlockText);
				System.Windows.Controls.Grid.SetRow(textBlockText, rowIndex);
				System.Windows.Controls.Grid.SetColumn(textBlockText, columnIndex);
			}
		}

		/// SetTableHeading
		public static void SetTableHeading(
			System.Windows.Controls.Grid grid,
			System.Int32 columnIndex,
			System.String headingText,
			System.Int32 rowIndex = 0)
		{
			SetTableHeading(grid, columnIndex, rowIndex);

			// test heading
			if (EFS.Common.Verify.String(headingText))
			{
				// create text block
				System.Windows.Controls.TextBlock textBlockText = new System.Windows.Controls.TextBlock();
				textBlockText.Foreground = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Black);
				textBlockText.Text = headingText;
				textBlockText.FontWeight = System.Windows.FontWeights.Bold;
				textBlockText.VerticalAlignment = System.Windows.VerticalAlignment.Center;
				textBlockText.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

				// add to grig
				grid.Children.Add(textBlockText);
				System.Windows.Controls.Grid.SetRow(textBlockText, 0);
				System.Windows.Controls.Grid.SetColumn(textBlockText, columnIndex);
			}
		}
	}

}